package main;

import color.ColorUI;
import filter.Repaint;
import watermark.WaterMark;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main extends JFrame implements ActionListener {
    private JButton Bfilter;
    private JButton Bcolor;
    private JButton Bwatermark;
    private JLabel title;
    /**
     * 程序入口
     * @param arg
     */
    public static void main(String[] arg){
        Main image=new Main();
        image.UImain();
    }

    /**
     * 各种package返回主界面的入口
     */
    public static void returnFunc(){
        Main image=new Main();
        image.UImain();
    }

    /**
     * 缩放图像（按高度和宽度缩放）
     * @param srcImageFile 源图像文件地址
     * @param result 缩放后的图像地址
     * @param height 缩放后的高度
     * @param width 缩放后的宽度
     * @param bb 比例不对时是否需要补白：true为补白; false为不补白;
     */
    public final static void scale2(String srcImageFile, String result, int height, int width, boolean bb) {
        try {
            double ratio = 0.0; // 缩放比例
            File f = new File(srcImageFile);
            BufferedImage bi = ImageIO.read(f);
            Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
            // 计算比例
            if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
                if (bi.getHeight() > bi.getWidth()) {
                    ratio = (new Integer(height)).doubleValue()
                            / bi.getHeight();
                } else {
                    ratio = (new Integer(width)).doubleValue() / bi.getWidth();
                }
                AffineTransformOp op = new AffineTransformOp(AffineTransform
                        .getScaleInstance(ratio, ratio), null);
                itemp = op.filter(bi, null);
            }
            if (bb) {//补白
                BufferedImage image = new BufferedImage(width, height,
                        BufferedImage.TYPE_INT_RGB);
                Graphics2D g = image.createGraphics();
                g.setColor(Color.white);
                g.fillRect(0, 0, width, height);
                if (width == itemp.getWidth(null))
                    g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
                            itemp.getWidth(null), itemp.getHeight(null),
                            Color.white, null);
                else
                    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
                            itemp.getWidth(null), itemp.getHeight(null),
                            Color.white, null);
                g.dispose();
                itemp = image;
            }
            ImageIO.write((BufferedImage) itemp, "JPEG", new File(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 窗体设计
     */
    public void UImain() {
        this.setTitle("图像处理");
        this.setResizable(false);
        this.setLayout(null);
        Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screensize.width,screensize.height-10);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //窗体长宽
        int fWidth=this.getWidth();
        int fHeight=this.getHeight();
        //按钮
        Bwatermark = new JButton();
        //获取一个图片
        ImageIcon square1=new ImageIcon("icon1.png");
        //设置按钮大小
        Bwatermark.setBounds((int)(0.08*fWidth),(int)(0.42*fHeight), (int)(0.19*fWidth), (int)(0.19*fWidth));
        //设置图片的大小
        square1.setImage(square1.getImage().getScaledInstance((int)(0.23*fWidth),(int)(0.23*fWidth), 0));
        //把图片放到按钮上
        Bwatermark.setIcon(square1);
        this.add(Bwatermark);
        Bwatermark.addActionListener(this);

        Bfilter = new JButton();
        ImageIcon square2=new ImageIcon("icon2.png");
        Bfilter.setBounds((int)(0.39*fWidth),(int)(0.42*fHeight), (int)(0.19*fWidth), (int)(0.19*fWidth));
        square2.setImage(square2.getImage().getScaledInstance((int)(0.23*fWidth),(int)(0.23*fWidth), 0));
        Bfilter.setIcon(square2);
        this.add(Bfilter);
        Bfilter.addActionListener(this);

        Bcolor = new JButton();
        ImageIcon square3=new ImageIcon("icon3.png");
        Bcolor.setBounds((int)(0.7*fWidth),(int)(0.42*fHeight), (int)(0.19*fWidth), (int)(0.19*fWidth));
        square3.setImage(square3.getImage().getScaledInstance((int)(0.23*fWidth),(int)(0.23*fWidth), 0));
        Bcolor.setIcon(square3);
        this.add(Bcolor);
        Bcolor.addActionListener(this);

        this.setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 设置监听器
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == Bwatermark) {
            dispose();
            WaterMark.mainWaterMark();
        }
        else if (e.getSource() == Bfilter) {
            dispose();
            Repaint.filter();
        }
        else if(e.getSource()==Bcolor){
            dispose();
            ColorUI.main();
        }
    }
}

